﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using System.Text;
/// <summary>
/// 数据处理工具类
/// </summary>
namespace SUNRPA.CONTROLS.IMPL.util {
    public class DataUtil {
        public static List<T> SliceList<T>(List<T> list, int startIndex, int endIndex) { 
            List<T> result = new List<T>();
            for(int i = startIndex; i < endIndex; i++) { 
                result.Add(list[i]);
            }
            return result;
        }

        public static string Btyes2String(byte[] bytes) { 
            return System.Text.Encoding.Default.GetString(bytes);
        }

        public static List<FlowItem> TempDeserialize(string jsonString) { 
            JsonSerializer serializer = new JsonSerializer();
            List<FlowItem> items = new List<FlowItem> ();
            JArray arr = JArray.Parse(jsonString);
            foreach(JObject obj in arr) { 
                Type controlType = Type.GetType(obj["ControlType"].ToString());
                if(controlType == null) {
                    FlowItem flowItem = new FlowItem();
                    serializer.Populate(obj.CreateReader(), flowItem);
                    items.Add(flowItem);
                } else { 
                    JObject cp = obj["ControlParam"] as JObject;
                    BaseParam param = Activator.CreateInstance(controlType) as BaseParam;
                    serializer.Populate(cp.CreateReader(), param);
                    FlowItem flowItem = new FlowItem();
                    serializer.Populate(obj.CreateReader(), flowItem);
                    flowItem.ControlParam = param;
                    items.Add(flowItem);
                }
            }
            return items;
        
        }

        public static string ObjectJsonSerializer(object data) { 
            JsonSerializerSettings set  = new JsonSerializerSettings();
            set.TypeNameHandling = TypeNameHandling.Auto;
            set.NullValueHandling = NullValueHandling.Ignore;
            return JsonConvert.SerializeObject(data, Formatting.Indented, set);
        }

        public static T DeSerializeJson<T>(string data) { 
            JsonSerializerSettings set  = new JsonSerializerSettings();
            set.TypeNameHandling = TypeNameHandling.Auto;
            set.NullValueHandling = NullValueHandling.Ignore;
            return JsonConvert.DeserializeObject<T>(data, set);
        }

        public static string GetTextFromStream(Stream stream){
            using(BufferedStream bs = new BufferedStream(stream)) {
                byte[] bytes = new byte[bs.Length];
                bs.Read(bytes, 0, bytes.Length);
                return Encoding.UTF8.GetString(bytes);
            }
        }
    }

    public abstract class JsonCreationConverter<T>:JsonConverter{
        protected abstract T Create(Type objectType, JObject jsonObject);
        public override bool CanConvert(Type objectType){
            return true;
        }
 
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer){
            var jsonObject = JObject.Load(reader);
            var target = Create(objectType, jsonObject);
            serializer.Populate(jsonObject.CreateReader(), target);
            return target;
        }
 
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer){
            throw new NotImplementedException();
        }
    }
    public class JsonConverterForParam:JsonCreationConverter<BaseParam> {
        protected override BaseParam? Create(Type objectType, JObject jsonObject) {
            string type = jsonObject["controlType"].ToString();
            Type t = Type.GetType(type);
            return Activator.CreateInstance(t) as BaseParam;
        }
    }
}
